// pages/mangox/getpackage/getpackage.js

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginInfo: {},
    pkh: "",
    depk: "",
    vdisplay: "none", //改变样式
    bdisplay: "none",
    liushuiList: {},
    sname: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {    
    //先登陆
    //auth.onLoadLogin();

    //this.showBusy();
    if (options && options.pkh) {
      this.setData({
        pkh: options.pkh,
        depk: options.depk
      });
    }
    this.setData({
      loginInfo: app.globalData.loginInfo
    });   
    this.GetPackage();
  },

  GetPackage: function (e) {
    let that = this;
    //未登录跳转去授权
    if (!app.globalData.loginInfo) {
      // wx.showModal({
      //   content: '请先微信授权再抢哦',
      //   showCancel: false
      // })

      wx.redirectTo({
        url: '/pages/user/auth/auth?pkh='+ that.data.pkh +'&depk='+ that.data.depk +''
      })
      return false;
    }

    wx.request({
      url: app.globalData.urlPath + '/api/Mangox/GetPackage',
      method: 'POST',
      data: {        
        Token: app.globalData.loginInfo.Token,
        Source: app.globalData.appSource,
        Pkgm: that.data.pkh,
        MgxHashID: app.globalData.loginInfo.Data.MgxHashID,
        UserName: app.globalData.loginInfo.Data.UserName,
      },
      success: function (res) {
        app.globalData.loginInfo.Token = res.data.TokenUpdate;
        console.log("抢金币返回：" + JSON.stringify(res.data));
        if(res.data.Code == 0){
          wx.showModal({
            content: '恭喜，成功抢到'+ res.data.Data.amount +'金币',
            showCancel: false,
            success: function (res) {
                if (res.confirm) {                  
                    console.log('用户点击确定')
                    //加载前立即执行
                    that.setData({
                      bdisplay: "block"
                    })

                    that.getttxs();

                    //加载成功后执行
                    that.setData({
                      vdisplay: "block"
                    })
                }
                else{}
            }
          });
        }
        else{            
          //加载前立即执行
          that.setData({
            bdisplay: "block"
          })

          if(res.data.Msg == "已抢光"){
            that.getttxs(res.data.Msg); 
          }
          else{
            that.getttxs(); 
          }

          that.setData({
            vdisplay: "block"
          })

        }
      }
    });
  },
  
  //查询历史记录 copy，gettxs.js
  getttxs: function (msg) {
    let tmsg = msg;
    let that = this;
    wx.request({
      url: app.globalData.urlPath + '/api/Mangox/Gettxs',
      method: 'POST',
      data: {
        Token: app.globalData.loginInfo.Token,
        //OpenID: app.globalData.openid,
        Source: app.globalData.appSource,
        Param: {
          userHash: that.data.depk,
          type: "1"
        },
      },
      success: function (res) {
        
        wx.showToast({
          title: tmsg,
          icon: "none",
          duration: 2000
        })

        app.globalData.loginInfo.Token = res.data.TokenUpdate;
        //转成json对象
        //var json = JSON.parse(res.data.Data.res_info);

        if (res && res.data && res.data.Data && res.data.Data.list && res.data.Data.list.length > 0) {
          console.log("查询历史记录成功,打一条日志：" + JSON.stringify(res.data.Data.list[0]));
          res.data.Data.list.forEach(item => {
            if (item.userName.length && item.userName.length > 12) {
              item.userName = item.userName.substring(item.userName.length-5);
            }
          });
        }
        else{
          console.log("查询历史记录无列表数据：" + JSON.stringify(res.data));
        }

        that.setData({
          liushuiList: res.data.Data.list,
          sname: res.data.Data.sname + "的红包",
        });
      }
    });
  },
  goIndex: function (){
    wx.switchTab({
      url: '/pages/index/index', 
      success: function (e) { } 
    })
  },

  showBusy: function () {
    wx.showToast({
      title: '加载中...',
      mask: true,
      duration: 3000,
      icon: 'loading'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})